In [1]:
import altair as alt
import pandas as pd
from vega_datasets import data

#This part of the plot is too large to submit if the output is generate, I am struggling with submiting to github as well...
# But it works, the plot is just not shown, u can run it. 

alt.data_transformers.disable_max_rows()

url = "https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/ufo-scrubbed-geocoded-time-standardized-00.csv"
df = pd.read_csv(url, header=None)

df.columns = [
    'Date_Time', 'City', 'State', 'Country', 'Shape', 'Duration', 
    'Duration_Reported', 'Comments', 'Date_Posted', 'Latitude', 'Longitude'
]

df['Date_Time'] = pd.to_datetime(df['Date_Time'], errors='coerce')
df['Year'] = df['Date_Time'].dt.year

df_us = (df[(df['Country'] == 'us') & 
            (~df['State'].str.lower().isin(['ak', 'hi']))]
         .assign(State=df['State'].str.upper()))

state_abbr_to_full = {
    "AL": "Alabama", "AZ": "Arizona", "AR": "Arkansas", "CA": "California",
    "CO": "Colorado", "CT": "Connecticut", "DE": "Delaware", "FL": "Florida", "GA": "Georgia",
    "ID": "Idaho", "IL": "Illinois", "IN": "Indiana", "IA": "Iowa",
    "KS": "Kansas", "KY": "Kentucky", "LA": "Louisiana", "ME": "Maine", "MD": "Maryland",
    "MA": "Massachusetts", "MI": "Michigan", "MN": "Minnesota", "MS": "Mississippi",
    "MO": "Missouri", "MT": "Montana", "NE": "Nebraska", "NV": "Nevada", "NH": "New Hampshire",
    "NJ": "New Jersey", "NM": "New Mexico", "NY": "New York", "NC": "North Carolina",
    "ND": "North Dakota", "OH": "Ohio", "OK": "Oklahoma", "OR": "Oregon", "PA": "Pennsylvania",
    "RI": "Rhode Island", "SC": "South Carolina", "SD": "South Dakota", "TN": "Tennessee",
    "TX": "Texas", "UT": "Utah", "VT": "Vermont", "VA": "Virginia", "WA": "Washington",
    "WV": "West Virginia", "WI": "Wisconsin", "WY": "Wyoming"
}
df_us["StateName"] = df_us["State"].map(state_abbr_to_full)


states_list = ['All States'] + sorted(df_us['StateName'].dropna().unique().tolist())
state_selector = alt.param(name='StateSelector', bind=alt.binding_select(options=states_list, name="Select State:"), value='All States')

us_map = alt.topo_feature(data.us_10m.url, 'states')

background = alt.Chart(us_map).mark_geoshape(
    fill='lightgray',
    stroke='white'
).transform_filter(
    'datum.id != 2 && datum.id != 15'
).properties(
    width=800,
    height=500
)

slider = alt.binding_range(min=df_us['Year'].min(), max=df_us['Year'].max(), step=1, name='Year:')
selection = alt.param(name='YearSelector', bind=slider, value=df_us['Year'].max())

points_nation = alt.Chart(df_us).mark_circle(size=30, color='red').encode(
    longitude='Longitude:Q',
    latitude='Latitude:Q',
    tooltip=[
        alt.Tooltip('City', title='City'),
        alt.Tooltip('State', title='State'),
        alt.Tooltip('Date_Time', title='Date/Time'),
        alt.Tooltip('Shape', title='Shape')
    ]
).add_params(
    selection
).transform_filter(
    'datum.Year == YearSelector'
)

main_map = background + points_nation

state_map = alt.Chart(us_map).mark_geoshape(
    fill='lightgray',
    stroke='black'
).transform_filter(
    'datum.properties.name == StateSelector && StateSelector != "All States"'
).add_params(
    state_selector
).properties(
    width=400,
    height=500
)

points_state = alt.Chart(df_us).mark_circle(size=80, color='blue').encode(
    longitude='Longitude:Q',
    latitude='Latitude:Q',
    tooltip=[
        alt.Tooltip('City', title='City'),
        alt.Tooltip('Date_Time', title='Date/Time'),
        alt.Tooltip('Shape', title='Shape')
    ]
).transform_filter(
    'datum.StateName == StateSelector && StateSelector != "All States" && datum.Year == YearSelector'
)

zoomed_map = state_map + points_state

final_map = alt.hconcat(
    main_map,
    zoomed_map
).resolve_scale(
    color='independent'
)

final_map
Out[1]:
In [2]:
import altair as alt
import pandas as pd
from vega_datasets import data



alt.data_transformers.disable_max_rows()

url = "https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/ufo-scrubbed-geocoded-time-standardized-00.csv"
df = pd.read_csv(url, header=None)

df.columns = [
    'Date_Time', 'City', 'State', 'Country', 'Shape', 'Duration', 
    'Duration_Reported', 'Comments', 'Date_Posted', 'Latitude', 'Longitude'
]
df['Date_Time'] = pd.to_datetime(df['Date_Time'], errors='coerce')
df['Year'] = df['Date_Time'].dt.year
df['Year_Group'] = (df['Year'] // 2) * 2 
df['Month'] = df['Date_Time'].dt.to_period('M')

if 'State' in df.columns:
    df['State'] = df['State'].str.upper()

state_abbr_to_full = {
    "AL": "Alabama", "AK": "Alaska", "AZ": "Arizona", "AR": "Arkansas", "CA": "California",
    "CO": "Colorado", "CT": "Connecticut", "DE": "Delaware", "FL": "Florida", "GA": "Georgia",
    "HI": "Hawaii", "ID": "Idaho", "IL": "Illinois", "IN": "Indiana", "IA": "Iowa",
    "KS": "Kansas", "KY": "Kentucky", "LA": "Louisiana", "ME": "Maine", "MD": "Maryland",
    "MA": "Massachusetts", "MI": "Michigan", "MN": "Minnesota", "MS": "Mississippi",
    "MO": "Missouri", "MT": "Montana", "NE": "Nebraska", "NV": "Nevada", "NH": "New Hampshire",
    "NJ": "New Jersey", "NM": "New Mexico", "NY": "New York", "NC": "North Carolina",
    "ND": "North Dakota", "OH": "Ohio", "OK": "Oklahoma", "OR": "Oregon", "PA": "Pennsylvania",
    "RI": "Rhode Island", "SC": "South Carolina", "SD": "South Dakota", "TN": "Tennessee",
    "TX": "Texas", "UT": "Utah", "VT": "Vermont", "VA": "Virginia", "WA": "Washington",
    "WV": "West Virginia", "WI": "Wisconsin", "WY": "Wyoming"
}
df['StateName'] = df['State'].map(state_abbr_to_full)

cleaned_df = df.dropna(subset=['Date_Time', 'StateName'])

states_list = ['All States'] + sorted(cleaned_df['StateName'].dropna().unique().tolist())

state_selector = alt.param(name='StateSelector', bind=alt.binding_select(options=states_list, name="Select State:"), value='All States')

year_groups = sorted(cleaned_df['Year_Group'].dropna().unique().tolist())

slider = alt.binding_range(min=min(year_groups), max=max(year_groups), step=2, name='Year Group:')
time_selector = alt.param(name='YearGroupSelector', bind=slider, value=max(year_groups))

annual_counts = cleaned_df.groupby(['Year_Group', 'StateName']).size().reset_index(name='Count')

nearest = alt.selection_point(
    on='mouseover', 
    nearest=True, 
    fields=['Year_Group'], 
    empty='none'
)

line_chart = alt.Chart(annual_counts).mark_line().encode(
    x=alt.X('Year_Group:O', title='Year (Grouped by 2 Years)'),
    y=alt.Y('Count:Q', title='Number of Reports'),
    color=alt.Color('StateName:N', title='State'),
    tooltip=[alt.Tooltip('Count:Q', title='Number of Reports')]
).transform_filter(
    '(StateSelector == "All States" || datum.StateName == StateSelector) && datum.Year_Group <= YearGroupSelector'
).add_params(
    state_selector,
    time_selector
).properties(
    width=800,
    height=400,
    title="UFO Sightings by State (Grouped by 2 Years)"
)

points = line_chart.mark_circle(size=80, color='red').encode(
    opacity=alt.condition(nearest, alt.value(1), alt.value(0))
).add_params(nearest)

final_chart = line_chart + points
final_chart
Out[2]:
In [3]:
alt.data_transformers.disable_max_rows()

url = "https://github.com/UIUC-iSchool-DataViz/is445_data/raw/main/ufo-scrubbed-geocoded-time-standardized-00.csv"
df = pd.read_csv(url, header=None)

df.columns = [
    'Date_Time', 'City', 'State', 'Country', 'Shape', 'Duration', 
    'Duration_Reported', 'Comments', 'Date_Posted', 'Latitude', 'Longitude'
]
df['Date_Time'] = pd.to_datetime(df['Date_Time'], errors='coerce')
df['Year'] = df['Date_Time'].dt.year
df['Year_Group'] = (df['Year'] // 2) * 2  

cleaned_df = df.dropna(subset=['Shape', 'State'])

shapes_list = sorted(cleaned_df['Shape'].dropna().unique().tolist())

year_groups = sorted(cleaned_df['Year_Group'].dropna().unique().tolist())

slider = alt.binding_range(min=min(year_groups), max=max(year_groups), step=2, name='Year Group:')
time_selector = alt.param(name='YearGroupSelector', bind=slider, value=max(year_groups))

shape_counts = cleaned_df.groupby(['Shape', 'Year_Group']).size().reset_index(name='Count')

bar_chart = alt.Chart(shape_counts).mark_bar().encode(
    x=alt.X('Shape:N', title='Shape', sort='-y'),
    y=alt.Y('Count:Q', title='Number of Reports'),
    color=alt.Color('Shape:N', title='UFO Shape', legend=alt.Legend(labelFontSize=14)),
    tooltip=['Shape', 'Year_Group', 'Count']
).transform_filter(
    'datum.Year_Group <= YearGroupSelector'
).add_params(
    time_selector
).properties(
    width=800,
    height=400,
    title="UFO Report Counts by Shape"
)

bar_chart
Out[3]:
In [ ]:
 
In [ ]: